package net.qiqb.usr.customer.adapter.web;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.qiqb.usr.custom.client.types.CustomerId;
import net.qiqb.usr.customer.adapter.web.req.*;
import net.qiqb.usr.customer.application.*;
import net.qiqb.usr.customer.infrastructure.CustomConstant;
import net.qiqb.usr.customer.infrastructure.persistence.dao.CustomDao;
import net.qiqb.usr.customer.infrastructure.persistence.dao.CustomerPo;
import net.qiqb.usr.points.application.CreatePointsCmd;
import net.qiqbframework.commandhandling.gateway.CommandGateway;
import net.qiqbframework.common.IdentifierFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户（customer）
 */
@RestController
@RequestMapping(CustomConstant.MAPPING_PREFIX_PATH + "/custom")
@Slf4j
@AllArgsConstructor
public class CustomController {

    private final CustomDao customDao;

    private final CommandGateway commandGateway;

    /**
     * 创建客户
     *
     * @param req
     * @return
     */
    @PostMapping("/create")

    public String create(@RequestBody CreatingCustomReq req) {
        final CreateCustomCmd cmd = req.generateCreateCustomCmd();
        commandGateway.sendAndWait(cmd);
        return "创建客户成功";
    }

    /**
     * 创建并发送
     *
     * @param req
     * @return
     */
    @PostMapping("/create_and_send")
    public String createAndSend(@RequestBody CreatingCustomReq req) {
        final CreateCustomCmd cmd = req.generateCreateCustomCmd();
        final CustomerId customerId = new CustomerId(IdentifierFactory.getInstance().generate());
        cmd.setId(customerId);
        // 在同一个事物里
        commandGateway.batchSendAndWait(List.of(cmd, new CreatePointsCmd(customerId)));
        return "创建客户并发布积分成功";
    }


    /**
     * 模拟批量新增客户
     *
     * @param req
     * @return
     */
    @PostMapping("/batch_create")
    public String batchCreate(@RequestBody CreatingCustomReq req) {
        final CreateCustomCmd cmd = req.generateCreateCustomCmd();
        List<CreateCustomCmd> mockBatch = new ArrayList<>();
        // 一次创建提交1w 用户
        for (int i = 0; i < 5000; i++) {
            CreateCustomCmd createCustomCmd = new CreateCustomCmd(cmd.getName() + i, cmd.getEmail());
            createCustomCmd.setRemarks(cmd.getRemarks());
            mockBatch.add(createCustomCmd);
        }

        commandGateway.batchSendAndWait(mockBatch);
        return "创建客户成功";
    }

    /**
     * 修改名称
     *
     * @param req
     * @return
     */
    @PostMapping("/change_name")
    public boolean changeName(@RequestBody ChangeCustomNameReq req) {
        //
        final ChangeCustomNameCmd cmd = req.generateChangeCustomNameCmd();
        commandGateway.sendAndWait(cmd);
        return true;
    }

    /**
     * 修改昵称
     *
     * @param req
     * @return
     */
    @PostMapping("/change_nickname")
    public boolean changeNameByName(@RequestBody ChangeCustomNicknameReq req) {
        //
        final ChangeCustomNicknameCmd cmd = req.generateChangeCustomNameCmd();
        commandGateway.sendAndWait(cmd);
        return true;
    }

    /**
     * 批量修改名称
     *
     * @param req
     * @return
     */
    @PostMapping("/batch_change_name")
    public String batchChangeName(@RequestBody BatchChangeCustomNameReq req) {
        // 批量修改用户名称
        List<ChangeCustomNameCmd> mockBatch = new ArrayList<>();
        final List<CustomerPo> customerPos = customDao.listAll();
        for (int i = 0; i < customerPos.size(); i++) {
            CustomerPo customerPo=customerPos.get(i);
            if (i ==-1){
                ChangeCustomNameCmd createCustomCmd = new ChangeCustomNameCmd(null, customerPo.getName() + "01");

                mockBatch.add(createCustomCmd);
            }else {
                ChangeCustomNameCmd createCustomCmd = new ChangeCustomNameCmd(new CustomerId(customerPo.getId()), customerPo.getName());

                mockBatch.add(createCustomCmd);
            }

        }


        commandGateway.batchSendAndWait(mockBatch);

        return "批量修改客户成功";
    }


    /**
     * 创建并启用客户
     *
     * @param req
     * @return
     */
    @PostMapping("/create_and_enable")
    public String createAndEnable(@RequestBody CreatingCustomReq req) {
        final CreateCustomCmd createCustomCmd = req.generateCreateCustomCmd();
        final EnableCustomCmd enableCustomCmd = new EnableCustomCmd(req.getName());
        commandGateway.batchSendAndWait(List.of(createCustomCmd, enableCustomCmd));
        return "创建客户并启用成功";
    }

    /**
     * 删除客户
     *
     * @param req
     * @return
     */
    @PostMapping("/delete")
    public String delete(@RequestBody DeleteCustomReq req) {
        final DeleteCustomCmd cmd = req.generateDeleteCustomCmd();
        commandGateway.sendAndWait(cmd);
        return "删除成功";
    }

}
